﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace personalplaner.common.controls
{
    [TemplatePart(Name = "PART_ButtonYes", Type = typeof(Button))]
    [TemplatePart(Name = "PART_ButtonNo", Type = typeof(Button))]
    [TemplatePart(Name = "PART_ButtonOk", Type = typeof(Button))]
    [TemplatePart(Name = "PART_ButtonCancel", Type = typeof(Button))]
	public class MessageBoxExt : WindowExt
	{
        private readonly Cursor _oldCursor;

        #region Construction

        static MessageBoxExt()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBoxExt), new FrameworkPropertyMetadata(typeof(MessageBoxExt)));
        }

		private MessageBoxExt()
		{
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            //WindowStyle = System.Windows.WindowStyle.ToolWindow;
            WindowStyle = System.Windows.WindowStyle.None;
            AllowsTransparency = true;

            _oldCursor = Mouse.OverrideCursor;
            Mouse.OverrideCursor = Cursors.Arrow;

            Loaded += new RoutedEventHandler(MessageBoxExt_Loaded);
		}

        #endregion

        #region Events

        void MessageBoxExt_Loaded(object sender, RoutedEventArgs e)
        {
            //Set focus to the default button

            if (_yesButton != null && _yesButton.IsDefault)
                _yesButton.Focus();
            else if (_noButton != null && _noButton.IsDefault)
                _noButton.Focus();
            else if (_okButton != null && _okButton.IsDefault)
                _okButton.Focus();
            else if (_cancelButton != null && _cancelButton.IsDefault)
                _cancelButton.Focus();
        }

        #endregion

        #region static MessageBoxExt calls

        public static MessageBoxResult Show(Window owner, object messageBoxContent, string caption)
        {
            var mb = new MessageBoxExt
            {
                Owner = owner,
                MessageBoxContent = messageBoxContent,
                Title = caption,
                MessageBoxButton = MessageBoxButton.OK
            };

            mb.ShowDialog();

            return mb.MessageBoxResult;
        }

        public static MessageBoxResult Show(Window owner, object messageBoxContent, string caption, MessageBoxButton button)
        {
            var mb = new MessageBoxExt
            {
                Owner = owner,
                MessageBoxContent = messageBoxContent,
                Title = caption,
                MessageBoxButton = button
            };

            mb.ShowDialog();

            return mb.MessageBoxResult;
        }

        public static MessageBoxResult Show(Window owner, object messageBoxContent, string caption, MessageBoxButton button,
                                               MessageBoxImage image)
		{
			var mb = new MessageBoxExt
			{
                Owner = owner,
				MessageBoxContent = messageBoxContent,
				Title = caption,
                MessageBoxButton = button,
                MessageBoxImage = image
			};

			mb.ShowDialog();

            return mb.MessageBoxResult;
		}
        
        public static MessageBoxResult Show(Window owner, object messageBoxContent, string caption,
                                               MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult)
        {

            var mb = new MessageBoxExt
            {
                Owner = owner,
                MessageBoxContent = messageBoxContent,
                Title = caption,
                MessageBoxButton = button,
                MessageBoxImage = image,
                MessageBoxResult = defaultResult
            };

            mb.ShowDialog();

            return mb.MessageBoxResult;
        }

        public static MessageBoxResult Show(Window owner, object messageBoxContent, string caption,
                                               MessageBoxButton button, MessageBoxImage image, MessageBoxResult defaultResult,
                                               MessageBoxOptions options)
        {
            var mb = new MessageBoxExt
            {
                Owner = owner,
                MessageBoxContent = messageBoxContent,
                Title = caption,
                MessageBoxButton = button,
                MessageBoxImage = image,
                MessageBoxResult = defaultResult,
                Options = options
            };

            mb.ShowDialog();

            return mb.MessageBoxResult;
        }

        #endregion

        #region DependencyProperties

        public object MessageBoxContent
        {
            get
            {
                return (object)GetValue(MessageBoxContentProperty);
            }
            set
            {
                SetValue(MessageBoxContentProperty, value);
            }
        }

        public static readonly DependencyProperty MessageBoxContentProperty =
            DependencyProperty.Register("MessageBoxContent", typeof(object), typeof(MessageBoxExt), new UIPropertyMetadata(null));


        public MessageBoxButton MessageBoxButton
        {
            get
            {
                return (MessageBoxButton)GetValue(MessageBoxButtonProperty);
            }
            set
            {
                SetValue(MessageBoxButtonProperty, value);
            }
        }

        public static readonly DependencyProperty MessageBoxButtonProperty =
            DependencyProperty.Register("MessageBoxButton", typeof(MessageBoxButton), typeof(MessageBoxExt), new UIPropertyMetadata(MessageBoxButton.OK));


        public MessageBoxImage MessageBoxImage
        {
            get
            {
                return (MessageBoxImage)GetValue(MessageBoxImageProperty);
            }
            set
            {
                SetValue(MessageBoxImageProperty, value);
            }
        }

        public static readonly DependencyProperty MessageBoxImageProperty =
            DependencyProperty.Register("MessageBoxImage", typeof(MessageBoxImage), typeof(MessageBoxExt), new UIPropertyMetadata(MessageBoxImage.None));
        
        #endregion

        #region Overrides

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            YesButton = GetTemplateChild("PART_ButtonYes") as Button;
            NoButton = GetTemplateChild("PART_ButtonNo") as Button;
            OkButton = GetTemplateChild("PART_ButtonOk") as Button;
            CancelButton = GetTemplateChild("PART_ButtonCancel") as Button;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        protected override void OnClosed(System.EventArgs e)
        {
            base.OnClosed(e);

            //Returns the cursor to the default state
            if (_oldCursor == null)
                Mouse.OverrideCursor = Cursors.Arrow;
            else
                Mouse.OverrideCursor = _oldCursor;
        }

        #endregion

        #region Buttons
        
        Button _yesButton;
        private Button YesButton
        {
            set
            {
                if (_yesButton != null)
                    _yesButton.Click -= new RoutedEventHandler(YesButtonClick);

                _yesButton = value;

                if(_yesButton!=null)
                    _yesButton.Click += new RoutedEventHandler(YesButtonClick);
            }
            get
            {
                return _yesButton;
            }
        }

        Button _noButton;
        private Button NoButton
        {
            set
            {
                if (_noButton != null)
                    _noButton.Click -= new RoutedEventHandler(NoButtonClick);

                _noButton = value;

                if(_noButton!=null)
                    _noButton.Click += new RoutedEventHandler(NoButtonClick);
            }
        }

        Button _okButton;
        private Button OkButton
        {
            set
            {
                if (_okButton != null)
                    _okButton.Click -= new RoutedEventHandler(OkButtonClick);

                _okButton = value;

                if(_okButton!=null)
                    _okButton.Click += new RoutedEventHandler(OkButtonClick);
            }
        }

        Button _cancelButton;
        private Button CancelButton
        {
            set
            {
                if (_cancelButton != null)
                    _cancelButton.Click -= new RoutedEventHandler(CancelButtonClick);

                _cancelButton = value;

                if(_cancelButton!=null)
                    _cancelButton.Click += new RoutedEventHandler(CancelButtonClick);
            }
        }

        #region Events
        
        void YesButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Yes;
            this.Close();
        }

        void NoButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.No;
            this.Close();
        }

        void OkButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.OK;
            this.Close();
        }

        void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBoxResult = MessageBoxResult.Cancel;
            this.Close();
        }

        #endregion

        #endregion

        #region Properties

        MessageBoxResult _messageBoxResult = MessageBoxResult.Cancel;
        public MessageBoxResult MessageBoxResult
        {
            get
            {
                return _messageBoxResult;
            }
            private set
            {
                _messageBoxResult = value;
            }
        }

        //Window _owner;
        //public Window Owner
        //{
        //    get
        //    {
        //        return _owner;
        //    }
        //    private set
        //    {
        //        _owner = value;
        //    }
        //}

        public new Window Owner
        {
            get
            {
                return base.Owner;
            }
            private set
            {

				if (value != null)
					base.Owner = value;
				else
				{
                    //if (RibbonWindowExt.IsShown)
						base.Owner = Application.Current.MainWindow;
                    //else
                    //    base.Owner = null;
				}

                //// ILA temporary
                //base.Owner = (value == null) ?
				//	Application.Current.MainWindow : value;
            }
        }

        MessageBoxOptions _messageBoxOptions = MessageBoxOptions.None;
        public MessageBoxOptions Options
        {
            get
            {
                return _messageBoxOptions;
            }
            private set
            {
                _messageBoxOptions = value;
            }
        }

        #endregion
    }
}
